#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <atomic>
#include <boost/algorithm/string.hpp>
using namespace std;

namespace util
{
    class PathUtil
    {
    public:
        PathUtil() {}
        ~PathUtil() {}
        static std::string AddSuffix(const std::string &filename, const std::string &suffix)
        {
            std::string temp_path = "../temp/";
            std::string path_name = temp_path;
            path_name += filename;
            path_name += suffix;
            return path_name;
        }
        static std::string Src(const std::string &filename)
        {
            return AddSuffix(filename, ".cpp");
        }
        static std::string Exe(const std::string &filename)
        {
            return AddSuffix(filename, ".exe");
        }
        static std::string CompileError(const std::string &filename)
        {
            return AddSuffix(filename, ".complie_error");
        }
        // 构建源文件路径+后缀的完整文件名
        // 编译时需要有的临时文件
        static std::string Stdin(const std::string &filename)
        {
            return AddSuffix(filename, ".stdin");
        }
        static std::string Stdout(const std::string &filename)
        {
            return AddSuffix(filename, ".stdout");
        }
        static std::string Stderror(const std::string &filename)
        {
            return AddSuffix(filename, ".stderr");
        }
    };
    class TimeUtil
    {
    public:
        TimeUtil() {};
        ~TimeUtil() {};
        static std::string GetTimeStamp()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec);
        }
        static std::string GetTimeMS()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }
    };
    class FileUtil
    {
    public:
        FileUtil() {};
        ~FileUtil() {};
        static bool IsFileExists(const std::string &filename)
        {
            struct stat st;
            if (stat(filename.c_str(), &st) == 0)
            {
                return true;
            }
            return false;
        }
        // target 不仅仅是 “文件名”，更准确地说，它是目标文件的 “路径 + 文件名” 组合—— 既可以包含文件所在的目录路径，也可以只包含单纯的文件名（取决于文件的位置）
        static bool WirteFile(const std::string &target, const std::string &content)
        {
            std::ofstream out(target);
            if (!out.is_open())
            {
                return false;
            }
            out.write(content.c_str(), content.size());
            out.close();
            return true;
        }
        static bool ReadFile(const std::string &target, std::string *content, bool keep)
        {
            std::ifstream in(target);
            if (!in.is_open())
            {
                return false;
            }
            std::string line;
            while (getline(in, line))
            {
                *content += line;
                *content += (keep ? "\n" : "");
            }
            in.close();
            return true;
        }
        static std::string UniqFIleName()
        {
            // 毫秒级时间戳+原子性递增唯一值: 来保证唯一性
            static atomic_uint id(0);
            id++;
            std::string ms = util::TimeUtil::GetTimeMS();
            std::string idstring = to_string(id);
            return ms + " " + idstring;
        }
    };
    class StringUtil
    {
    public:
        static void SplitString(const std::string &str, std::vector<std::string> *target, const std::string &sep)
        {
            boost::split((*target), str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}